package controller

import (
	"context"
	"github.com/gin-gonic/gin"
	"net/http"
	"order_bff/order"
	"order_bff/pkg"
	"strconv"
)

func CreateCar(c *gin.Context) {
	name := c.PostForm("name")
	types := c.PostForm("types")
	image := c.PostForm("image")
	nums := c.PostForm("nums")
	numsInt, _ := strconv.Atoi(nums)
	price := c.PostForm("price")
	priceFlaoat, _ := strconv.ParseFloat(price, 10)
	if name == "" || types == "" || image == "" || len(nums) == 0 || len(price) == 0 {
		c.JSON(http.StatusNotFound, gin.H{
			"code": -1,
			"msg":  "参数不能为空",
			"data": nil,
		})
		return
	}
	comment := pkg.Comment()
	_, err := comment.CreateCar(context.Background(), &order.CreateCarRequest{
		Name:  name,
		Type:  types,
		Image: image,
		Nums:  int32(numsInt),
		Price: float32(priceFlaoat),
	})
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{
			"code": -1,
			"msg":  "创建车型失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": nil,
	})
}

func CarDetail(c *gin.Context) {
	carId := c.Query("car_id")
	carIdInt, _ := strconv.Atoi(carId)
	if len(carId) == 0 {
		c.JSON(http.StatusNotFound, gin.H{
			"code": -1,
			"msg":  "ID不能为空",
			"data": nil,
		})
		return
	}
	comment := pkg.Comment()
	detail, err := comment.CarDetail(context.Background(), &order.CarDetailRequest{CarId: int32(carIdInt)})
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{
			"code": -1,
			"msg":  "获取车型详情失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": detail,
	})
}
